package com.hnas.account.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.hnas.account.orm.Account;
import com.hnas.account.service.dto.DTOAccount;
import com.hnas.account.service.dto.search.DTOAccountSearch;
import com.hnas.account.service.mapper.AccountMapper;
import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.dao.PageBean;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.util.DateUtil;
import com.hnas.core.util.StringUtil;
import com.hnas.ecard.dao.AccECardDAO;
import com.hnas.ecard.dao.CardTrailDAO;
import com.hnas.ecard.domain.CardTrailStatus;
import com.hnas.ecard.domain.ECardStatus;
import com.hnas.ecard.orm.AccECard;
import com.hnas.ecard.orm.CardTrail;

@Component("accountDAO")
public class AccountDAOImpl extends BaseDaoImpl<Account> implements AccountDAO {
	
	@Autowired
	private CardTrailDAO cardTrailDAO;
	
	@Autowired
	private AccECardDAO accECardDAO;

	public AccountDAOImpl() {
		super(Account.class);
	}

	@Override
	public Account saveOrUpdate(DTOAccount dto) throws Exception {
		Account vo = null;
		if(dto != null){
			String id = dto.getId();
			if(StringUtil.isNotEmpty(id)){
				vo = this.find(id);
			}
			if(vo == null){//新增
				vo = new Account();
				BeanUtils.copyProperties(dto, vo);
				this.persist(vo);
			}else{//修改
				BeanUtils.copyProperties(dto, vo, new String[]{"createTime","creator"});
				vo.setUpdateTime(DateUtil.dateTimeToString(new Date()));
				this.merge(vo);
			}
		}
		return vo;
	}

	@Override
	public DTOPagedList<DTOAccount> search(DTOAccountSearch criteria) {
		DTOPagedList<DTOAccount> pageList = new DTOPagedList<DTOAccount>();
		List<DTOAccount> list = new ArrayList<DTOAccount>();
		PageBean<Account> page = new PageBean<Account>();
		page.setPageNo(criteria.getPageNo());
		page.setPageSize(criteria.getPageSize());
		page.setTotalCount(criteria.getTotalCount());
		StringBuffer sb = new StringBuffer();
		List<Object> paras = new ArrayList<Object>();
		int j = 1;
		if(StringUtil.isNotEmpty(criteria.getPhoneNo())){
			sb.append(" and t.phoneNo = ?"+j++);
			paras.add(criteria.getPhoneNo().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getEmpId())){
			sb.append(" and t.empId = ?"+j++);
			paras.add(criteria.getEmpId().trim());
		}
		
		if(StringUtil.isNotEmpty(criteria.getValid())){
			sb.append(" and t.valid = ?"+j++);
			paras.add(criteria.getValid().trim());
		}
		if(StringUtil.isNotEmpty(criteria.getStartDate())){
			sb.append(" and t.lastLoginTime >= ?"+j++);
			paras.add(criteria.getStartDate().trim()+" 00:00:00");
		}
		
		if(StringUtil.isNotEmpty(criteria.getEndDate())){
			sb.append(" and t.lastLoginTime <= ?"+j++);
			paras.add(criteria.getEndDate().trim()+" 23:59:59");
		}
		
		if(sb.length() > 0){
			sb.delete(0, 5);
			sb.insert(0, " from Account t  where ");
		}else{
			sb.append(" from Account t ");
		}
		sb.append(" order by t.lastLoginTime desc");
		page.setQuery(sb.toString());
		page.setQueryCount("select count(t.id) "+sb);
		this.queryByPage(page, paras.toArray());
		pageList.setPageNo(page.getPageNo());
		pageList.setPageSize(page.getPageSize());
		pageList.setTotalCount(page.getTotalCount());
		for(Account vo : page.getList()){
			DTOAccount dto = new DTOAccount();
			dto = AccountMapper.getInstance().mapToValue(vo);
			dto = this.setCardInfo(dto);
			list.add(dto);
		}
		pageList.setList(list);
		return pageList;
	}

	@Override
	public List<Account> findAll() {
		String hql = "from Account t";
		return this.findByQuery(hql);
	}

	@Override
	public DTOAccount setCardInfo(DTOAccount dto) {
		if(null != dto){
			dto.setEleCardNum("0");
			dto.setUnclaimedEleCardNum("0");
			//查询电子卡数量、待确认的卡数量
			List<CardTrail> unclaimedCards = cardTrailDAO.findCardsByStatus("", dto.getPhoneNo(), CardTrailStatus.RECEIVING_GIVING);
			AccECard ecard = new AccECard();
			ecard.setAccId(dto.getId());
			ecard.setStatus(ECardStatus.NORMAL);
			List<AccECard> VcardCards = accECardDAO.findByECardSql(ecard);
			if(!CollectionUtils.isEmpty(unclaimedCards)){
				dto.setUnclaimedEleCardNum(unclaimedCards.size()+"");
			}
			if(!CollectionUtils.isEmpty(VcardCards)){
				dto.setEleCardNum(VcardCards.size()+"");
			}
		}
		return dto;
	}

}
